package com.str.array;
public class ArrayOps { 

  // Insert val at index ins in the array
  public static void insert(Object[] a, int left, int right,
      Object val, int pos) {
    if (pos < left || pos > right) 
      throw new IllegalStateException();
    for (int i = right; i > pos; i--)
      a[i] = a[i - 1];
    a[pos] = val;
  }

  // Delete the value at index del in the array
  public static void delete(Object[] a, int left, int right,
      int pos) {
    if (pos < left || pos > right) 
      throw new IllegalStateException();
    for (int i = pos; i < right; i++)
      a[i] = a[i+1];
    a[right] = null;
  }

  // Print the contents of the array as a String
  public static void print(Object[] a, int left, int right) {
    System.out.print("[ ");
    for (int i = left; i <= right; i++)
      System.out.print(a[i] + " ");
    System.out.println("]");
  }

  public static void main(String args[]) {
    String[] words = {"fox", "cow", "pig", "cat", "rat", "lio",
                      "tig", "goa", "dog"};
    
    System.out.print("Initially array: ");
    print(words, 0, words.length-1);
    
    insert(words, 0, words.length-1, "ant", 7);
    System.out.print("insert \"ant\" at index 7: ");
    print(words, 0, words.length-1);
    
    insert(words, 0, words.length-1, "eag", 4);
    System.out.print("insert \"eag\" at index 4: ");
    print(words, 0, words.length-1);
    
    delete(words, 0, words.length-1, 5);
    System.out.print("delete value at index 5: ");
    print(words, 0, words.length-1);                  
  }   
}
